<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/03/20 12:08:25 $)
// $RCSfile: staff_dashboard.php,v $ : $Revision: 1.14 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$template->loadLanguageSection("dashboard");
if ($module->isRegistered(MODULE_TICKETS))
{
	$template->loadLanguageSection("staff_tickets");
	require_once ("./modules/tickets/tickets.php");
	require_once ("./modules/tickets/functions_ticketsui.php");
	renderTicketTree();
}

if (empty($_REQUEST["type"]))
{
	$_type = "today";
} else if ($_REQUEST["type"] == "news") {
	$_type = "news";
}


$template->assign("isstaffdashboard", true);
$template->assign("calendar", true);
$template->assign("itemoptiontitle", $_SWIFT["language"]["quickinsert"]);

// Tickets
if ($module->isRegistered(MODULE_TICKETS) && $_SWIFT["staff"]["t_entab"] != "0")
{
	$interface->assignItemOption($_SWIFT["language"]["ticket"], "", "icon_ticket.gif", "tuserticket", '');
}

// Teamwork
if ($module->isRegistered(MODULE_TEAMWORK) && $_SWIFT["staff"]["tw_entab"] != "0")
{
	$interface->assignItemOption($_SWIFT["language"]["contact"], "index.php?_m=teamwork&_a=insertcontact", "icon_contacts.gif");
	$interface->assignItemOption($_SWIFT["language"]["event"], "index.php?_m=teamwork&_a=insertevent", "icon_event.gif");
	$interface->assignItemOption($_SWIFT["language"]["task"], "index.php?_m=teamwork&_a=inserttask", "icon_task.gif");
}

// Knowledgebase
if ($module->isRegistered(MODULE_KNOWLEDGEBASE) && $_SWIFT["staff"]["kb_entab"] != "0")
{
	$interface->assignItemOption($_SWIFT["language"]["article"], "index.php?_m=knowledgebase&_a=insertquestion", "icon_knowledgebase.gif");
}

// Downloads
if ($module->isRegistered(MODULE_DOWNLOADS) && $_SWIFT["staff"]["dl_entab"] != "0")
{
	$interface->assignItemOption($_SWIFT["language"]["file"], "index.php?_m=downloads&_a=insertfile", "mimeico_blank.gif");
}

// News
if ($module->isRegistered(MODULE_NEWS) && $_SWIFT["staff"]["nw_entab"] != "0")
{
	$interface->assignItemOption($_SWIFT["language"]["news"], "index.php?_m=news&_a=insertnews", "icon_newsitem.gif");
}

// Users
if ($_SWIFT["staff"]["cu_entab"] != "0")
{
	$interface->assignItemOption($_SWIFT["language"]["user"], "index.php?_m=core&_a=insertuser", "icon_user.gif");
}

$interface->staffHeader($_SWIFT["language"]["dashboard"], 1);

$interface->staffNavBar($_SWIFT["language"]["dashboard"], "", 1);

?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top">

<div class="dashboardtitle"><?=iif($_type=="today", $_SWIFT["language"]["dashtoday"], $_SWIFT["language"]["dashnewstitle"])?></div>
<?
echo '<table cellspacing="0" cellpadding="0" border="0" width="100%" id="caltableopt">'.SWIFT_CRLF;
echo '<tr style="height: 1em;"><td align="left"><div id="tab_main" style="DISPLAY: block;" class="tabcontent">'.SWIFT_CRLF;
?>
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="dashborder">
<tr style="height: 1em">
<td align="left" valign="top">

<table cellspacing="0" cellpadding="2" height="300" border="0" width="100%" class="dashcontent">
<tr><td align="left" valign="top">

	<?
	$_datestart = mktime(0,0,0, date("m", DATENOW), date("d", DATENOW), date("Y", DATENOW));
	$_dateend = mktime(23, 59, 99, date("m", DATENOW), date("d", DATENOW), date("Y", DATENOW));
	if ($module->isRegistered(MODULE_TICKETS) && $_type == "today")
	{
		$_assigns = $_overduetickets = $_ticketsduetoday = $_overdueticketidlist = array();
		$_assigns = getAssignedDepartments(MODULE_TICKETS);

		// Get Overdue Tickets
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."tickets` WHERE `departmentid` IN (". buildIN($_assigns) .") AND `ticketstatusid` IN (". buildIN(getDisplayTicketStatusList()) .") AND (`duetime` < '". DATENOW ."' AND `duetime` != 0);");
		while ($dbCore->nextRecord())
		{
			if (empty($dbCore->Record["ownerstaffid"]) || $dbCore->Record["ownerstaffid"] == $_SWIFT["staff"]["staffid"])
			{
				$_overduetickets[$dbCore->Record["ticketid"]] = $dbCore->Record;
				$_overdueticketidlist[] = $dbCore->Record["ticketid"];
			}
		}

		// Get Tickets Due Today
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."tickets` WHERE `departmentid` IN (". buildIN($_assigns) .") AND `ticketstatusid` IN (". buildIN(getDisplayTicketStatusList()) .") AND (`duetime` > '". intval($_datestart) ."' AND `duetime` < '". intval($_dateend) ."');");
		while ($dbCore->nextRecord())
		{
			if ((empty($dbCore->Record["ownerstaffid"]) || $dbCore->Record["ownerstaffid"] == $_SWIFT["staff"]["staffid"]) && !in_array($dbCore->Record["ticketid"], $_overdueticketidlist))
			{
				$_ticketsduetoday[$dbCore->Record["ticketid"]] = $dbCore->Record;
			}
		}

		if (count($_overduetickets))
		{
			?>
			<table width="100%"  border="0" cellspacing="1" cellpadding="2"><tr>
			<td width="100" class="darkredtext" nowrap><?=$_SWIFT["language"]["overduetickets"]?></td>
			<td valign="middle"><div class="linediv"><img src="<?=$_SWIFT["themepath"]?>space.gif" height="1" width="1" /></div></td></tr>
			</table>
			<table width="100%"  border="0" cellspacing="1" cellpadding="2">
			<?
			foreach ($_overduetickets as $key=>$val)
			{
				?>
				<tr>
				<td width="1"><img src="<?=$_SWIFT["themepath"]?>icon_redbigdot.gif" border="0" /></td>
				<td valign="middle" align="left" class="smalltext"><a href="index.php?_m=tickets&_a=viewticket&ticketid=<?=$val["ticketid"]?>"><?=iif($_SWIFT["settings"]["t_eticketid"]=="seq",$val["ticketid"], $val["ticketmaskid"])?>: <?=htmlspecialchars($val["subject"])?></a></td>
				<td width="100" align="right" class="smalltext"><?=htmlspecialchars($val["lastreplier"])?></td>
				</tr>
				<?
			}
			?></table><?
		}

		if (count($_ticketsduetoday))
		{
			?>
			<table width="100%"  border="0" cellspacing="1" cellpadding="2"><tr>
			<td width="110" class="smalltext" nowrap><?=$_SWIFT["language"]["ticketsduetoday"]?></td>
			<td valign="middle"><div class="linediv"><img src="<?=$_SWIFT["themepath"]?>space.gif" height="1" width="1" /></div></td></tr>
			</table>
			<table width="100%" border="0" cellspacing="1" cellpadding="2">
			<?
			foreach ($_ticketsduetoday as $key=>$val)
			{
				?>
				<tr>
				<td width="1"><img src="<?=$_SWIFT["themepath"]?>icon_yellowbigdot.gif" border="0" /></td>
				<td valign="middle" align="left" class="smalltext"><a href="index.php?_m=tickets&_a=viewticket&ticketid=<?=$val["ticketid"]?>"><?=iif($_SWIFT["settings"]["t_eticketid"]=="seq",$val["ticketid"], $val["ticketmaskid"])?>: <?=htmlspecialchars($val["subject"])?></a></td>
				<td width="100" align="right" class="smalltext"><?
				if ($val["duetime"] != 0)
				{
					echo strColorDate(($val["duetime"]-DATENOW));
				}
				?></td>
				<td width="100" align="right" class="smalltext"><?=htmlspecialchars($val["lastreplier"])?></td>
				</tr>
				<?
			}
			?></table><?
		}
	}

	if ($module->isRegistered(MODULE_TEAMWORK) && $_type == "today")
	{
		// Get all due tasks

		$_duetasks = $_taskstoday = $_todaytaskidlist = array();

		//MAHESH: Displays due tasks in all staff members dashboard. 
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendartasks` WHERE (`duedate` >= '". intval($_datestart) ."' AND `duedate` < '". intval($_dateend) ."') AND `iscomplete` = '0' AND `tasktype` != 'private';");
		while ($dbCore->nextRecord())
		{
			$_taskstoday[$dbCore->Record["calendartaskid"]] = $dbCore->Record;
			$_todaytaskidlist[] = $dbCore->Record["calendartaskid"];
		}

		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendartasks` WHERE `ownerstaffid` = '". intval($_SWIFT["staff"]["staffid"]) ."' AND (`duedate` = '0' OR `duedate` < '". DATENOW ."') AND `iscomplete` = '0' AND `tasktype` != 'private';");
		while ($dbCore->nextRecord())
		{
			if (in_array($dbCore->Record["calendartaskid"], $_todaytaskidlist))
			{
				continue;
			}

			$_duetasks[$dbCore->Record["calendartaskid"]] = $dbCore->Record;
		}

		if (count($_duetasks))
		{
			?>
			<table width="100%"  border="0" cellspacing="1" cellpadding="2"><tr>
			<td width="120" class="darkredtext" nowrap><?=$_SWIFT["language"]["dashduetasks"]?></td>
			<td valign="middle"><div class="linediv"><img src="<?=$_SWIFT["themepath"]?>space.gif" height="1" width="1" /></div></td></tr>
			</table>
			<table width="100%" border="0" cellspacing="1" cellpadding="2">
			<?
			foreach ($_duetasks as $key=>$val)
			{
				?>
				<tr>
				<td width="1"><img src="<?=$_SWIFT["themepath"]?>icon_task.gif" border="0" /></td>
				<td valign="middle" align="left" class="smalltext"><a href="index.php?_m=teamwork&_a=edittask&calendartaskid=<?=$val["calendartaskid"]?>"><?=htmlspecialchars($val["subject"])?></a></td>
					<td width="100" align="right" class="smalltext"><?=iif($val["staffid"]!=$_SWIFT["staff"]["staffid"], $_SWIFT["staffcache"][$val["staffid"]]["fullname"]);?></td>
					<td width="100" align="right" class="smalltext"><?
					if ($val["duedate"] != 0 && $val["duedate"] > DATENOW)
					{
						echo strColorDate(($val["duedate"]-DATENOW));
					} else {
						echo $_SWIFT["language"]["undated"];
					}
					?></td>
					</tr>
					<?
			}
			?></table><?
		}

		if (count($_taskstoday))
		{
			?>
			<table width="100%"  border="0" cellspacing="1" cellpadding="2"><tr>
			<td width="120" class="darkredtext" nowrap><?=$_SWIFT["language"]["tasksduetoday"]?></td>
			<td valign="middle"><div class="linediv"><img src="<?=$_SWIFT["themepath"]?>space.gif" height="1" width="1" /></div></td></tr>
			</table>
			<table width="100%" border="0" cellspacing="1" cellpadding="2">
			<?
			foreach ($_taskstoday as $key=>$val)
			{
				?>
				<tr>
				<td width="1"><img src="<?=$_SWIFT["themepath"]?>icon_task.gif" border="0" /></td>
				<td valign="middle" align="left" class="smalltext"><a href="index.php?_m=teamwork&_a=edittask&calendartaskid=<?=$val["calendartaskid"]?>"><?=htmlspecialchars($val["subject"])?></a></td>
					<td width="100" align="right" class="smalltext"><?=iif($val["staffid"]!=$_SWIFT["staff"]["staffid"], $_SWIFT["staffcache"][$val["staffid"]]["fullname"]);?></td>
					<td width="100" align="right" class="smalltext"><span class="tickettextblue"><?
					echo $_SWIFT["language"]["twtoday"];
					?></span></td>
					</tr>
					<?
			}
			?></table><?
		}


		// ======= BEGIN STATISTICS STUFF =======
		if ($module->isRegistered(MODULE_TICKETS))
		{
			$departmentidlist = getAssignedDepartments(MODULE_TICKETS);
			$_newtickets = 0;
			$_ticketstatusidlist = getDisplayTicketStatusList();

			$_tcountlist = $dbCore->queryFetch("SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."tickets` WHERE `departmentid` IN (". buildIN($departmentidlist) .") AND `ticketstatusid` IN (". buildIN($_ticketstatusidlist) .") AND `lastactivity` > ". intval($_SWIFT["staff"]["lastvisit"]) .";");
			$_ticketcount = intval($_tcountlist["totalitems"]);
		}

		if ($module->isRegistered(MODULE_LIVESUPPORT))
		{
			$departmentidlist = getAssignedDepartments(MODULE_LIVESUPPORT);
			$_newchats = 0;

			$_chatcount = $dbCore->queryFetch("SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."chatobjects` WHERE `departmentid` IN (". buildIN($departmentidlist) .") AND `dateline` > '". intval($_SWIFT["staff"]["lastvisit"]) ."';");
			$_newchats = intval($_chatcount["totalitems"]);
		}

		?>
		<table width="100%"  border="0" cellspacing="1" cellpadding="2"><tr>
		<td width="60" class="smalltext" nowrap><?=$_SWIFT["language"]["statistics"]?></td>
		<td valign="middle"><div class="linediv"><img src="<?=$_SWIFT["themepath"]?>space.gif" height="1" width="1" /></div></td></tr>
		</table>
		<table width="100%" border="0" cellspacing="1" cellpadding="2">
		<?
		if ($module->isRegistered(MODULE_TICKETS))
		{
			?>
			<tr class="smalltext">
			<td width="1" nowrap><img src="<?=$_SWIFT["themepath"]?>icon_ticketblue.gif" border="0" align="absmiddle"></td>
			<td width="100" nowrap><?=$_SWIFT["language"]["dnewtickets"]?></td>
			<td><a href="index.php?_m=tickets&_a=search&type=newtickets" id="moduletitle"><?=$_ticketcount?></a></td>
			</tr>
		<?
		}
		if ($module->isRegistered(MODULE_LIVESUPPORT))
		{
			?>
			<tr class="smalltext">
			<td width="1" nowrap><img src="<?=$_SWIFT["themepath"]?>icon_chat.gif" border="0" align="absmiddle"></td>
			<td width="100" nowrap><?=$_SWIFT["language"]["dnewchats"]?></td>
			<td><?=$_newchats?></td>
			</tr>
		<?
		}
		?>
		</table><?
	}

	if ($module->isRegistered(MODULE_NEWS) && $_type == "news")
	{
		$_news = array();
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."news` WHERE `newstype` = '". SWIFTPRIVATE ."' AND (`expiry` > '". DATENOW ."' OR `expiry` = '0') ORDER BY `newsid` DESC;");
		while ($dbCore->nextRecord())
		{
			$_news[$dbCore->Record["newsid"]] = $dbCore->Record;
		}

		if (count($_news))
		{
			?>
			<table width="100%"  border="0" cellspacing="0" cellpadding="0" class="smalltext">
			<?
			foreach ($_news as $key=>$val)
			{
				?>
				<tr><td align="left" valign="top" colspan="2" style="PADDING: 3px;"><a href="javascript:popupDataWindow('index.php?_m=news&_a=viewnews&newsid=<?=$val["newsid"]?>');" id="newstitle"><img src="<?=$_SWIFT["themepath"]?>icon_newsitembig.gif" border="0" align="absmiddle" />&nbsp;<?=$val["subject"]?></a></td></tr>
				<tr><td align="left" valign="top" colspan="2" style="PADDING: 3px;"><span class="newsdate"><?=$_SWIFT["language"]["nwpostedby"]?><?=$_SWIFT["staffcache"][$val["staffid"]]["fullname"]?> <?=$_SWIFT["languaged"]["nwpostedon"]?><?=edate($_SWIFT["settings"]["dt_datetimeformat"], $val["dateline"])?></span></td></tr>
				<tr><td align="left" valign="top" colspan="2" style="PADDING: 3px;"><span class="smalltext"><?=$val["description"]?></span></td></tr>
				<tr><td align="left" valign="top" style="PADDING: 3px;"><span class="smalltext"><a href="javascript:popupDataWindow('index.php?_m=news&_a=viewnews&newsid=<?=$val["newsid"]?>');"><img src="<?=$_SWIFT["themepath"]?>icon_readnews.gif" border="0" align="absmiddle" />&nbsp;<?=$_SWIFT["language"]["nwread"]?></a></span></td><td align="right" valign="top"><span class="smalltext"></span></td></tr>
				<tr><td height="4" colspan="2" background="<?=$_SWIFT["themepath"]?>linerepeat.gif"><img src="<?=$_SWIFT["themepath"]?>space.gif" width="1" height="4"></td></tr>
				<?
			}
			?></table><?
		}
	}

	?>




</td></tr>
</table>
</td>
</tr>
</table>
</div>
</td></tr>
<?
	echo '<tr style="height: 1em"><td align="left"><div><ul id="btab">
	<li><a'. iif($_type=="today", ' class="currenttab"') .' href="index.php?_m=core&_a=dashboard" onClick="javascript:void(0);" id="dashtoday" title="'. $_SWIFT["language"]["dashtoday2"] .'">'. $_SWIFT["language"]["dashtoday2"] .'</a></li>';
	if ($module->isRegistered(MODULE_NEWS) && $_SWIFT["settings"]["nw_entab"] == 1)
	{
		$_newscount = $dbCore->queryFetch("SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."news` WHERE `newstype` = '". SWIFTPRIVATE ."' AND (`expiry` > '". DATENOW ."' OR `expiry` = '0')");
		echo '<li><a'. iif($_type=="news", ' class="currenttab"') .' href="index.php?_m=core&_a=dashboard&type=news" id="dashnews" title="'. sprintf($_SWIFT["language"]["dashnews"], intval($_newscount["totalitems"])) .'">'. sprintf($_SWIFT["language"]["dashnews"], intval($_newscount["totalitems"])) .'</a></li>';
	}
	echo '</ul></div></td></tr>'.SWIFT_CRLF;
?>
</table>
</td>
<td align="right" valign="top" width="5"><img src="<?=$_SWIFT["themepath"]?>space.gif" border="0" width="5" height="5" /></td>
<?
if ($module->isRegistered(MODULE_TEAMWORK) && $_SWIFT["staff"]["tw_entab"] != "0")
{
	require_once ("./modules/teamwork/functions_calendarrender.php");
?>
<td align="right" valign="top" width="40%"><div class="dashboardtitle">&nbsp;</div>
<?

	echo '<table cellspacing="0" cellpadding="0" border="0" width="100%" id="caltableopt">'.SWIFT_CRLF;

	echo '<tr style="height: 1em"><td align="left">'.SWIFT_CRLF;
	$dayresult = renderDayView(false, true);
	echo '<div id="tab_twday" style="DISPLAY: block;" class="tabcontent">'. $dayresult .'</div>';
	echo '</td></tr>';

	echo '<tr style="height: 1em"><td align="left"><div><ul id="btab">
	<li><a class="currenttab" href="#" onClick="javascript:void(0);" id="twday" title="'. $_SWIFT["language"]["day"] .'">'. $_SWIFT["language"]["day"] .'</a></li>
	<li><a href="index.php?_m=teamwork&_a=calendar&calendartype=workweek" id="twworkweek" title="'. $_SWIFT["language"]["workweek"] .'">'. $_SWIFT["language"]["workweek"] .'</a></li>
	<li><a href="index.php?_m=teamwork&_a=calendar&calendartype=week" id="twweek" title="'. $_SWIFT["language"]["week"] .'">'. $_SWIFT["language"]["week"] .'</a></li>
	<li><a href="index.php?_m=teamwork&_a=calendar&calendartype=month" id="twmonth" title="'. $_SWIFT["language"]["month"] .'">'. $_SWIFT["language"]["month"] .'</a></li>
	</ul></div></td></tr>'.SWIFT_CRLF;

	echo '</table>';
?>
</td>
<?
}
?>
</tr>
</table>
<?

$interface->staffFooter();
?>